package com.jackie.controller;

import com.jackie.common.AjaxResult;
import com.jackie.common.Global;
import com.jackie.exception.UserException;
import com.jackie.model.User;
import com.jackie.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * Created by jackie on 2016/5/17.
 */
@Controller
public class UserController {
    private Logger log = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 通过验证session里的用户信息
     * 检查当前用户是否已登录
     *
     * @param session
     * @return
     */
    @RequestMapping("/check")
    public
    @ResponseBody
    String check(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (null != user)
            return user.getUserName();
        log.info("用户没有在session中");
        return null;
    }

    /**
     * 用户登陆操作
     *
     * @param userName
     * @param userPassword
     * @param session   登陆成功则写入session
     * @return 登陆成功则返回用户名，失败则返回空字符串
     */
    @RequestMapping("/login")
    public String login(
            @RequestParam("userName") String userName, @RequestParam("userPassword") String userPassword,
            HttpSession session, HttpServletResponse response) {
        User user = userService.login(userName);
        if (null != user && userPassword.equals(user.getUserPassword())) {
            //验证成功
            session.setAttribute("user", user);
            Cookie name = new Cookie("userName", userName);
            name.setPath("/ssm/");
            name.setMaxAge(Global.DEFALUT_COOKIE_MAXAGE);//1小时
            response.addCookie(name);
            log.info("用户" + user.getUserName() + "登陆成功");
            return "redirect:index.jsp";
        } else if (null == user) {
            log.info("用户" + userName + "登陆失败,用户名不存在");
            throw new UserException("用户名不存在");
        } else if (!userPassword.equals(user.getUserPassword())) {
            log.info("用户" + userName + "登陆失败,密码错误");
            throw new UserException("密码错误");
        } else {
            log.info("用户" + userName + "登陆失败,未知原因");
            throw new UserException("未知异常，请联系管理员：15737733633");
        }
    }

    /**
     * @param user
     * @return 返回首页
     */
    @RequestMapping("/register")
    @ResponseBody
    public AjaxResult register(User user) {
        user.setResourceId(UUID.randomUUID().toString());
        user.setIsDeleted(0);
        user.setVersion(Global.VERSION);
        int result = userService.register(user);
        if (result == 0) {
            return new AjaxResult(false, "注册失败,用户名已存在");
        }
        return new AjaxResult(true, "注册成功，即将跳转到首页");
    }

    @RequestMapping("/logout")
    @ResponseBody
    public AjaxResult logout(HttpSession session) {
        session.removeAttribute("user");
        AjaxResult result = new AjaxResult(true, "退出成功");
        return result;
    }
}
